package com.fin.zw.aiqas.controller;

import com.fin.zw.aiqas.annotation.CurrentUser;
import com.fin.zw.aiqas.model.data.InspectionDetailModel;
import com.fin.zw.aiqas.model.request.InspectionDetailQueryRequest;
import com.fin.zw.aiqas.model.request.InspectionDetailRequest;
import com.fin.zw.aiqas.model.request.RuleHistRequest;
import com.fin.zw.aiqas.model.response.InspectionDetailQueryResponse;
import com.fin.zw.aiqas.model.session.SessionModel;
import com.fin.zw.aiqas.service.InspectionDetailService;
import com.fin.zw.aiqas.util.FileUtil;
import com.github.pagehelper.PageHelper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.util.List;

import static org.springframework.web.bind.annotation.RequestMethod.PATCH;
import static org.springframework.web.bind.annotation.RequestMethod.POST;

@RestController
@RequestMapping(value = "/inspectionDetail")
@Api(value = "质检结果详情", description = "质检结果详情")
public class InspectionDetailController {

    @Autowired
    InspectionDetailService inspectionDetailService;
    private final static Logger log = LoggerFactory.getLogger(InspectionDetailController.class);



    @RequestMapping(value = "/update", method = {POST, PATCH}, name = "addInspectionDetail")
    @ApiOperation(value = "根据明细Id修改质检结果明细", notes = "根据明细Id修改质检结果明细")
    public ResponseEntity<Void> updateInspectionDetail(@RequestBody InspectionDetailRequest request,
                                                       @ApiIgnore @CurrentUser SessionModel session) {
        try {
            inspectionDetailService.updateInspectionDetail(request, session);
            return ResponseEntity.ok().build();
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
            return ResponseEntity.badRequest().build();
        }
    }


    @GetMapping(value = "/queryPage")
    @ApiImplicitParams({
            @ApiImplicitParam(name="page", dataType = "int", paramType = "query", value = "页数（0..N）"),
            @ApiImplicitParam(name = "size", dataType = "int", paramType = "query", value = "每页大小."),
            @ApiImplicitParam(name = "sort", allowMultiple = true, dataType = "string", paramType = "query", value = "依据什么排序: 属性名(asc|desc). "),
    })
    @ApiOperation(value = "根据质检结果Id查询质检结果明细", notes = "根据质检结果Id查询质检结果明细")
    public ResponseEntity<Page<InspectionDetailQueryResponse>> queryInspectionDetail(InspectionDetailQueryRequest request, Pageable pageable) {
        Page<InspectionDetailQueryResponse> list = inspectionDetailService.queryInspectionDetailById(request, pageable);
        return ResponseEntity.ok().body(list);
    }

    @ApiOperation(value = "导出质检结果信息", notes = "导出质检结果信息")
    @GetMapping("/exportInspectionDetailData")
    public void exportData(HttpServletResponse response, InspectionDetailQueryRequest request) {
        log.info("文件导出开始........");
        File file = inspectionDetailService.exportFile(request);
        FileUtil.download(response, file);
        file.delete();
        log.info("文件制作完成开始下载........");
    }

    @GetMapping(value = "/queryInspectionDetailModelByRuleId")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", dataType = "int", paramType = "query", value = "页数（0..N）"),
            @ApiImplicitParam(name = "size", dataType = "int", paramType = "query", value = "每页大小."),
            @ApiImplicitParam(name = "sort", allowMultiple = true, dataType = "string", paramType = "query", value = "依据什么排序: 属性名(asc|desc). "),
    })
    @ApiOperation(value = "根据规则id查询命中质检结果", notes = "根据规则id查询命中质检结果")
    public ResponseEntity<Page<InspectionDetailModel>> queryInspectionDetailModelByRuleId(RuleHistRequest request, Pageable pageable) {
        Integer count = inspectionDetailService.queryCountInspectionDetailModelByRuleId(request);
        PageHelper.startPage(pageable.getPageNumber() + 1, pageable.getPageSize());
        List<InspectionDetailModel> list = inspectionDetailService.queryInspectionDetailModelByRuleId(request);
        Page<InspectionDetailModel> pageResult = new PageImpl<>(list, pageable, count);
        return ResponseEntity.ok().body(pageResult);
    }
}
